<template>
  <!-- 历史任务管理 -->
  <div style="position: relative;">
    <el-card style="height: calc( 100vh - 85px); overflow:auto; ">
      <!-- 信息搜索框 -->
      <el-form v-model="queryObj" label-width="100px" size="large">
        <el-row>
          <el-col :span="7" style="margin-right:15px">
            <el-form-item label="容器编号：">
              <el-input v-model.trim="queryObj.unitId" placeholder="请输入容器编号" />
            </el-form-item>
          </el-col>
          <el-col :span="7" style="margin-right:15px">
            <el-form-item label="主任务类型：">
              <el-select v-model="queryObj.mainTaskType" style="width:100%" placeholder="请选择主任务类型">
                <el-option v-for="item in queryObj.mainTaskType" :key="item" :value="item">{{ item }}</el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col v-show="isopen" :span="7" style="margin-right:15px">
            <el-form-item label="任务编号：">
              <el-input v-model.trim="queryObj.taskId" placeholder="请输入任务编号" />
            </el-form-item>
          </el-col>
          <el-col v-show="isopen" :span="7" style="margin-right:15px">
            <el-form-item label="发送者：">
              <el-input v-model.trim="queryObj.sender" placeholder="请输入发送者" />
            </el-form-item>
          </el-col>
          <el-col v-show="isopen" :span="7" style="margin-right:15px">
            <el-form-item label="优先级：">
              <el-input v-model.trim="queryObj.priority" placeholder="请输入优先级" />
            </el-form-item>
          </el-col>
          <el-col v-show="isopen" :span="7" style="margin-right:15px">
            <el-form-item label="起始点：">
              <el-input v-model.trim="queryObj.fromLoc" placeholder="请输入起始点" />
            </el-form-item>
          </el-col>
          <el-col v-show="isopen" :span="7" style="margin-right:15px">
            <el-form-item label="目的点：">
              <el-input v-model.trim="queryObj.toLoc" placeholder="请输入目的点" />
            </el-form-item>
          </el-col>

          <el-col v-show="isopen" :span="7" style="margin-right:15px;">
            <el-form-item label="时间：">
              <el-date-picker v-model="timestart_end" style="width:100%" type="datetimerange" start-placeholder="开始日期" end-placeholder="结束日期" format="yyyy/MM/dd HH:mm:ss"
                value-format="yyyy/MM/dd HH:mm:ss" @change="handleDataChanged" range-separator="至" :default-time="['00:00:00', '23:59:59']" />
            </el-form-item>
          </el-col>
          <searchbutton :isopen="isopen" @isopenclickfn="isopenclick" @resetQueryfn="resetQuery" @queryClickfn="queryClick" />
        </el-row>
      </el-form>

      <!-- 调整状态按钮 -->
      <el-row style="margin-bottom:10px">
        <div style="float:left" />
        <!-- 右侧公用按钮功能 -->
        <div style="float:right">
          <div style="font-size:30px;display: inline-block;margin-right:5px">
            <a href="#">
              <el-tooltip class="item" effect="dark" content="列位置" placement="top" style="cursor:pointer">
                <el-dropdown trigger="click" :hide-on-click="false">
                  <span class="el-dropdown-link">
                    <i class="el-icon-s-grid el-icon--right" style="font-size:24px;color:black" />
                  </span>
                  <el-dropdown-menu slot="dropdown" style=" width:190px;height:300px;overflow:auto">
                    <el-dropdown-item><el-checkbox v-model="deleteCell.istaskId">任务编号</el-checkbox></el-dropdown-item>
                    <el-dropdown-item><el-checkbox v-model="deleteCell.issender">sender</el-checkbox></el-dropdown-item>
                    <el-dropdown-item><el-checkbox v-model="deleteCell.ismainTaskType">主任务类型</el-checkbox></el-dropdown-item>
                    <el-dropdown-item><el-checkbox v-model="deleteCell.isunitId">容器编号</el-checkbox></el-dropdown-item>
                    <el-dropdown-item><el-checkbox v-model="deleteCell.isfromLoc">起始点</el-checkbox></el-dropdown-item>
                    <el-dropdown-item><el-checkbox v-model="deleteCell.istoLoc">目的点</el-checkbox></el-dropdown-item>
                    <el-dropdown-item><el-checkbox v-model="deleteCell.istaskStatus">任务状态</el-checkbox></el-dropdown-item>
                    <el-dropdown-item><el-checkbox v-model="deleteCell.isactiveSubTaskId">当前子任务</el-checkbox></el-dropdown-item>
                    <el-dropdown-item><el-checkbox v-model="deleteCell.istotalSubTaskNum">子任务总数</el-checkbox></el-dropdown-item>
                    <el-dropdown-item><el-checkbox v-model="deleteCell.isextend">extend</el-checkbox></el-dropdown-item>
                    <el-dropdown-item><el-checkbox v-model="deleteCell.isreasonCode">reasonCode</el-checkbox></el-dropdown-item>
                    <el-dropdown-item><el-checkbox v-model="deleteCell.ispriority">任务优先级</el-checkbox></el-dropdown-item>
                    <el-dropdown-item><el-checkbox v-model="deleteCell.iscreatedTime">创建时间</el-checkbox></el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-tooltip>
            </a>
          </div>
          <!-- 右侧公用按钮功能组件 -->
          <functionButton style="float:right" :refresh-click="refreshClick" :table-data="tableData" :table-data-all="tableDataAll" :headers-arr="headersArr" :headers-relations="headersRelations"
            @exportAllfn="exportAll" />
        </div>
      </el-row>

      <!-- 任务总表 -->
      <el-table v-if="tableData.taskId !== ''" v-loading="listLoading" :data="tableData" border :row-style="{height:'40px'}" :cell-style="{padding:'0px'}"
        style="width: 100%;  margin-bottom: 10px;overflow:auto;">
        <!-- 子任务下拉表格 -->
        <el-table-column type="expand" style="overflow:auto">
          <template slot-scope="scope">
            <!-- {{ scope.$index }} - {{ scope.row }} -->
            <!-- {{ scope.row.subTasks }} -->
            <el-table :data="scope.row.subTasks" :row-style="{height:'40px'}" :cell-style="{padding:'0px'}" border>
              <el-table-column prop="taskSeq" label="序号" align="center" width="50" />
              <el-table-column prop="taskFrom" label="任务来源" width="120" />
              <el-table-column prop="subTaskId" label="子任务编号" width="150" />
              <el-table-column prop="subTaskType" label="子任务类型" width="180" />
              <el-table-column prop="unitId" label="容器编号" width="180" />
              <el-table-column prop="fromLoc" label="起始点" width="180" />
              <el-table-column prop="toLoc" label="目的点" width="180" />
              <el-table-column prop="taskStatus" label="任务状态" width="180" />
              <el-table-column prop="plcName" label="plc名" width="180" />
            </el-table>
          </template>
        </el-table-column>
        <el-table-column v-if="deleteCell.istaskId" prop="taskId" label="任务编号" width="160" show-overflow-tooltip>
          <template slot-scope="scope">
            <!-- :data-clipboard-text 复制的内容 -->
            <span class="copydemo" :data-clipboard-text="scope.row.taskId" @click="copy">{{ scope.row.taskId }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="deleteCell.issender" prop="sender" label="sender" width="100" />
        <el-table-column v-if="deleteCell.ismainTaskType" prop="mainTaskType" label="主任务类型" width="160" />
        <el-table-column v-if="deleteCell.isunitId" prop="unitId" label="容器编号" width="150" />
        <el-table-column v-if="deleteCell.ispriority" prop="priority" label="任务优先级" width="110" />
        <el-table-column v-if="deleteCell.isfromLoc" prop="fromLoc" label="起始点" width="130" />
        <el-table-column v-if="deleteCell.istoLoc" prop="toLoc" label="目的点" width="130" />
        <el-table-column v-if="deleteCell.istaskStatus" prop="taskStatus" label="任务状态" width="100" />
        <el-table-column v-if="deleteCell.isactiveSubTaskId" prop="activeSubTaskId" label="当前子任务" width="100" />
        <el-table-column v-if="deleteCell.istotalSubTaskNum" prop="totalSubTaskNum" label="子任务总数" width="100" />
        <el-table-column v-if="deleteCell.isextend" prop="extend" label="extend" width="130" />
        <el-table-column v-if="deleteCell.isreasonCode" prop="reasonCode" label="reasonCode" width="130" />
        <el-table-column v-if="deleteCell.iscreatedTime" prop="createdTime" label="创建时间" width="160" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ scope.row.createdTime | formatDate }}</span>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <paging :pagion-obj="pagionObj" :handle-size-change="handleSizeChange" :handle-page-change="handlePageChange" />
    </el-card>
  </div>
</template>

<script>
import { gettaskbakdataapi } from "@/api/task";
import { copyData } from "@/utils/index";
export default {
  name: "TaskBakPage",
  data() {
    return {
      queryObj: {
        unitId: "", // 容器编号
        mainTaskType: "", // 主任务类型
        sender: "", // 任务来源
        taskId: "", // 任务编号
        priority: "", // 优先级
        fromLoc: "", // 起始点
        toLoc: "", // 目的点
        extend: "",
        reasonCode: "",
        activeSubTaskId: 0, // 当前子任务
        totalSubTaskNum: 0, // 子任务总数totalSubTaskNum
        taskStatus: 0, // 任务状态
        // subTasks: [], // 子任务
        page: 0, // 第几页
        size: 0, // 每页条数
        startTime: "0001/1/1 0:00:00", // 开始日期
        endTime: "0001/1/1 0:00:00", // 结束日期
      },
      // 分页的数据
      pagionObj: {
        size: 10,
        currentPage: 1,
        total: 100,
      },
      tableData: [],
      tableDataAll: [],
      // 开始日期结束日期的数组
      timestart_end: [],
      isopen: false,
      // 页面loding效果
      listLoading: true,
      // 列位置数据
      deleteCell: {
        istaskId: true,
        issender: true,
        ismainTaskType: true,
        isunitId: true,
        isfromLoc: true,
        istoLoc: true,
        istaskStatus: true,
        isactiveSubTaskId: true,
        istotalSubTaskNum: true,
        isextend: true,
        isreasonCode: true,
        ispriority: true,
        iscreatedTime: true,
      },
      headersArr: [
        "id",
        "sender",
        "任务编号",
        "起始点",
        "目的点",
        "容器编号",
        "任务优先级",
        "extend",
        "reasonCode",
        "主任务类型",
        "当前子任务",
        "子任务总数务",
        "创建时间",
        "任务状态",
        // '子任务'
      ],
      // 将表头转换成中文
      headersRelations: {
        id: "id",
        sender: "sender",
        任务编号: "taskId",
        起始点: "fromLoc",
        目的点: "toLoc",
        容器编号: "unitId",
        任务优先级: "priority",
        extend: "extend",
        reasonCode: "reasonCode",
        主任务类型: "mainTaskType",
        当前子任务: "activeSubTaskId",
        子任务总数务: "totalSubTaskNum",
        创建时间: "createdTime",
        任务状态: "taskStatus",
        // '子任务': 'subTasks'
      },
    };
  },
  // 监听时间的变化
  watch: {
    timestart_end(newData) {
      if (newData !== null && newData.length > 0) {
        this.queryObj.startTime = this.timestart_end[0];
        this.queryObj.endTime = this.timestart_end[1];
      } else {
        this.queryObj.startTime = "0001/1/1 0:00:00";
        this.queryObj.endTime = "0001/1/1 0:00:00";
      }
    },
  },

  mounted() {
    this.queryData();
  },

  methods: {
    // 获取历史任务数据
    async queryData() {
      this.listLoading = true;
      this.queryObj.size = this.pagionObj.size;
      this.queryObj.page = this.pagionObj.currentPage;
      const res = await gettaskbakdataapi(this.queryObj);
      this.tableData = res.data.data;
      // console.log(res, 1111111)
      // console.log(this.tableData, 222222)
      // console.log(this.tableData[0].subTasks, 33333)
      this.pagionObj.pageCount = res.data.pageCount;
      this.pagionObj.total = res.data.count;
      this.listLoading = false;
    },
    // 搜索按钮
    queryClick() {
      this.pagionObj.currentPage = 1;
      this.queryData();
    },
    // 刷新按钮
    refreshClick() {
      this.listLoading = true;
      this.queryData();
      this.listLoading = false;
    },
    /** 每页显示数据变化*/
    handleSizeChange(val) {
      this.pagionObj.size = val;
      this.queryData();
    },
    /** 当前页变化*/
    handlePageChange(val) {
      this.pagionObj.currentPage = val;
      this.queryData();
    },
    // 时间选择器处理
    handleDataChanged() {
      if (this.timestart_end !== null) {
        this.queryObj.startTime = this.timestart_end[0];
        this.queryObj.endTime = this.timestart_end[1];
      } else {
        this.queryObj.startTime = "0001/1/1 0:00:00";
        this.queryObj.endTime = "0001/1/1 0:00:00";
      }
    },
    // 重置按钮
    resetQuery() {
      this.queryObj.unitId = "";
      this.queryObj.mainTaskType = "";
      this.queryObj.sender = "";
      this.queryObj.taskId = "";
      this.queryObj.priority = "";
      this.queryObj.fromLoc = "";
      this.queryObj.toLoc = "";
      this.queryObj.extend = "";
      this.queryObj.reasonCode = "";
      this.timestart_end = [];
    },
    // 展开收起
    isopenclick() {
      this.isopen = !this.isopen;
    },
    // 复制
    copy() {
      copyData();
    },
    // excel导出全部
    async exportAll() {
      const res2 = await gettaskbakdataapi({ size: 10000 });
      this.tableDataAll = res2.data.data;
    },
  },
};
</script>
<style lang="scss" scoped>
</style>
